package com.stox.client.ui.javafx.chartingy.persistence.chart.widget;

import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.util.Callback;

import com.stox.client.ui.core.model.intf.HasNavigable;
import com.stox.client.ui.core.model.intf.Navigable;
import com.stox.client.ui.javafx.core.util.CSS;
import com.stox.client.ui.javafx.widget.ListViewNavigableDecorator;
import com.stox.shared.charting.model.SavedChart;

public class SavedChartListView extends ListView<SavedChart> implements HasNavigable<SavedChart> {

	public SavedChartListView() {
		getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
		VBox.setVgrow(this, Priority.ALWAYS);
		setCellFactory(new Callback<ListView<SavedChart>, ListCell<SavedChart>>() {
			@Override
			public ListCell<SavedChart> call(ListView<SavedChart> param) {
				return new ListCell<SavedChart>(){
					
					{
						getStyleClass().add(CSS.savedChartListCell);
					}
					
					@Override
					protected void updateItem(SavedChart item, boolean empty) {
						super.updateItem(item, empty);
						if(!empty && null != item){
							setText(item.getName());
						}else{
							setText(null);
						}
					}
				};
			}
		});
		setStyle();
	}

	private void setStyle(){
		getStyleClass().add(CSS.savedChartListView);
	}
	
	@Override
	public Navigable<SavedChart> getNavigable() {
		return new ListViewNavigableDecorator<>(this, SavedChart.class);
	}

}
